#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <vector>

class OrderedPrinter
{
private:
    int current_num;            // 当前要打印的数字
    int thread_count;           // 线程数量
    std::mutex mtx;             // 互斥锁
    std::condition_variable cv; // 条件变量

public:
    OrderedPrinter(int thread_count) : current_num(1), thread_count(thread_count) {}

    void print(int thread_id)
    {
        while (true)
        {
            std::unique_lock<std::mutex> lock(mtx);
            // 等待直到轮到当前线程打印
            cv.wait(lock, [&]()
                    { return current_num > 100 || (current_num - 1) % thread_count == thread_id; });

            if (current_num > 100)
            {
                lock.unlock();
                cv.notify_all(); // 通知所有线程退出
                return;
            }

            // 打印数字
            std::cout << "Thread " << thread_id << ": " << current_num << std::endl;
            current_num++;

            // 通知下一个线程
            lock.unlock();
            cv.notify_all();
        }
    }

    void run()
    {
        std::vector<std::thread> threads;
        for (int i = 0; i < thread_count; ++i)
        {
            threads.emplace_back(&OrderedPrinter::print, this, i);
        }
        for (auto &t : threads)
        {
            t.join();
        }
    }
};

int main()
{
    const int thread_count = 3; // 线程数量
    OrderedPrinter printer(thread_count);
    printer.run();
    return 0;
}